//
//  ViewController.m
//  TouchesOnScreen
//
//  Created by iD Student on 7/12/13.
//  Copyright (c) 2013 iD Student. All rights reserved.
//

#import "ViewController.h"

@interface ViewController ()
{
    NSArray *imageArray;
    NSMutableArray *screenImagesArray;
    NSMutableArray *labelArray;
}
@end

@implementation ViewController
{
    int diameter;
}

- (void)viewDidLoad
{
    [super viewDidLoad];
	// Do any additional setup after loading the view, typically from a nib.
    imageArray = [[NSArray alloc]initWithObjects:
                  [UIImage imageNamed:@"ButtonRing1.png"],
                  [UIImage imageNamed:@"ButtonRing2.png"],
                  [UIImage imageNamed:@"ButtonRing3.png"],
                  [UIImage imageNamed:@"ButtonRing4.png"],
                  [UIImage imageNamed:@"ButtonRing5.png"], nil];
    screenImagesArray = [[NSMutableArray alloc]init];
    self.view.multipleTouchEnabled = TRUE;
    diameter = 50;
    labelArray = [[NSMutableArray alloc]init];
}

- (void) touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event
{
    if(labelArray.count > 0)
    {
    for (int i = 0; i < touches.count; i++) {
        [[labelArray objectAtIndex:i]removeFromSuperview];
        [labelArray removeObjectAtIndex:i];
    }
    }
    NSArray *touchesArray = [[NSArray alloc]initWithArray:[touches allObjects]];
    for (int i = 0; i <touchesArray.count; i++) {
        NSLog([NSString stringWithFormat:@"X:%f Y:%f", [[touchesArray objectAtIndex:i] locationInView:self.view].x,[[touchesArray objectAtIndex:i]locationInView:self.view].y]);
    }
    for (int i = 0; i < screenImagesArray.count; i++) {
        [[screenImagesArray objectAtIndex:i]removeFromSuperview];
    }
    
    NSMutableArray *touchArray = [NSMutableArray arrayWithArray:[touches allObjects]];
    for (int i = 0; i < touchArray.count; i++) {
        CGPoint touchPoint = [[touchArray objectAtIndex:i] locationInView:self.view];
        if (i <= 5) {
            UIImageView *touchImageView = [[UIImageView alloc]initWithImage:[imageArray objectAtIndex:(i)]];
            touchImageView.frame = CGRectMake(touchPoint.x - (diameter/2.0), touchPoint.y - (diameter/2.0), diameter, diameter);
            [self.view addSubview:touchImageView];
            [screenImagesArray addObject:touchImageView];
            UILabel *label = [[UILabel alloc]initWithFrame:touchImageView.frame];
            label.text = [NSString stringWithFormat:@"%i", (i + 1)];
            [self.view addSubview:label];
            label.backgroundColor = [UIColor clearColor];
            label.textAlignment = UITextAlignmentCenter;
            [labelArray addObject:label];
        } else {
            UIImageView *touchImageView = [[UIImageView alloc]initWithImage:[imageArray objectAtIndex:(i - 5)]];
            touchImageView.frame = CGRectMake(touchPoint.x - (diameter / 2.0), touchPoint.y - (diameter/2.0), diameter, diameter);
            [self.view addSubview:touchImageView];
            [screenImagesArray addObject:touchImageView];
            UILabel *label = [[UILabel alloc]initWithFrame:touchImageView.frame];
            label.text = [NSString stringWithFormat:@"%i", (i + 1)];
            [self.view addSubview:label];
            label.backgroundColor = [UIColor clearColor];
            label.textAlignment = UITextAlignmentCenter;
            [labelArray addObject:label];
        }
    }
}

- (void) touchesMoved:(NSSet *)touches withEvent:(UIEvent *)event
{
    if(labelArray.count > 0)
    {
        for (int i = 0; i < touches.count; i++) {
            [[labelArray objectAtIndex:i]removeFromSuperview];
            [labelArray removeObjectAtIndex:i];
        }
    }
    NSMutableArray *touchArray = [NSMutableArray arrayWithArray:[touches allObjects]];
    for (int i = 0; i < touchArray.count; i++) {
        CGPoint touchPoint = [[touchArray objectAtIndex:i] locationInView:self.view];
        if (i <= 5) {
            UIImageView *touchImageView = [[UIImageView alloc]initWithImage:[imageArray objectAtIndex:(i)]];
            touchImageView.frame = CGRectMake(touchPoint.x - (diameter/2.0), touchPoint.y - (diameter/2.0), diameter, diameter);
            [self.view addSubview:touchImageView];
            [screenImagesArray addObject:touchImageView];
            UILabel *label = [[UILabel alloc]initWithFrame:touchImageView.frame];
            label.text = [NSString stringWithFormat:@"%i", (i + 1)];
            [self.view addSubview:label];
            label.backgroundColor = [UIColor clearColor];
            label.textAlignment = UITextAlignmentCenter;
            [labelArray addObject:label];
        } else {
            UIImageView *touchImageView = [[UIImageView alloc]initWithImage:[imageArray objectAtIndex:(i - 5)]];
            touchImageView.frame = CGRectMake(touchPoint.x - (diameter / 2.0), touchPoint.y - (diameter/2.0), diameter, diameter);
            [self.view addSubview:touchImageView];
            [screenImagesArray addObject:touchImageView];
            UILabel *label = [[UILabel alloc]initWithFrame:touchImageView.frame];
            label.text = [NSString stringWithFormat:@"%i", (i + 1)];
            [self.view addSubview:label];
            label.backgroundColor = [UIColor clearColor];
            label.textAlignment = UITextAlignmentCenter;
            [labelArray addObject:label];
        }
    }

}

- (void)didReceiveMemoryWarning
{
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

- (void) resetScreen:(id)sender
{
    
}

@end
